package vn.vietel.pms.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;

import vn.vietel.pms.entity.VKindOfProperty;

public class KindTypeConverter implements javax.faces.convert.Converter {
	private List<VKindOfProperty> mTypes;

	public KindTypeConverter(List<VKindOfProperty> types) {
		this.mTypes = types;
	}

	public String getAsString(FacesContext ctx, UIComponent comp, Object obj) {
		if (obj == null)
			return null;
		VKindOfProperty type = (VKindOfProperty) obj;
		String val = String.valueOf(type.getId());
		return val;
	}

	public Object getAsObject(FacesContext ctx, UIComponent comp, String strVal)
			throws ConverterException {
		if (strVal == null || strVal.length() == 0) {
			return null;
		}
		long id = Long.valueOf(strVal).longValue();
		for (VKindOfProperty type : mTypes) {
			if (type.getId().longValue() == id) {
				return type;
			}
		}
		return null;
	}

}